package com.hongning.railway.ui.main.fragments.dynamic;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hongning.railway.BR;
import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.databinding.FragmentDynamicBinding;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.ViewModelFactory;


public class DynamicFragment extends BaseFragment<FragmentDynamicBinding,DynamicViewModel> {

    public static DynamicFragment newInstance() {
        return new DynamicFragment();
    }


    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_dynamic;
    }

    @Override
    public int initVariableId() {
        return BR.dynamicmodel;
    }

    @Override
    public DynamicViewModel initViewModel() {
        AppViewModelFactory viewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,viewModelFactory).get(DynamicViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        Apputils.setDefaultHeaderView(binding.trRefhreh);
        binding.topInclude.tvTopTitle.setText("党建云动态");
        binding.topInclude.ivRight.setVisibility(View.VISIBLE);
        binding.topInclude.ivRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setVisiable();
            }
        });
        binding.rvContent.setItemAnimator(null);
        viewModel.getCategoryListData();
    }
    @Override
    public void initViewObservable() {
        viewModel.uc.finishRefreshing.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.finishRefreshing();
                binding.trRefhreh.setEnableLoadmore(true);
//                binding.trRefhreh.setHeaderView();
            }
        });

        viewModel.uc.finishLoadmore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.finishLoadmore();
            }
        });

        viewModel.uc.enableLoadmore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.setEnableLoadmore(false);
            }
        });
    }
}
